package com.fyzk.view.mylistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tyjc.lottery.R;
import com.fyzk.utils.Globle;

public class FooterView extends LinearLayout{

	public final static int FOOTER_OPTIONS_PULL = 0;  
    public final static int FOOTER_OPTIONS_CLICK = 1;  
    /**
     * 更新方式：  点击、拖动
     */
    private  int sFooterOps = FOOTER_OPTIONS_PULL;  
      
    public final static int STATE_NORMAL = 0;  
    public final static int STATE_WILL_RELEASE = 1;  
    public final static int STATE_LOADING = 2;  
    /**
     * 状态：未拖动、拖动未松手、松手后
     */
    private int mState = STATE_NORMAL;  
      
    private View mFooter = null;  
    /**
     * 箭头图标
     */
    private ImageView mArrow = null;  
    /**
     * 等待对话框
     */
    private ProgressBar mProgressBar = null; 
    /**
     * 提示文字信息
     */
    private TextView mLoaderTips = null;  
    
    /**
     * 动画效果 上
     */
    private RotateAnimation mRotateUp = null;  
    /**
     * 动画效果 下
     */
    private RotateAnimation mRotateDown = null; 
    /**
     * 动画间歇时间
     */
    private final static int ROTATE_DURATION = 250;  
      
    public FooterView(Context context) {  
        this(context, null);  
    }  
      
    public FooterView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        initFooterView(context);  
    } 
	
	public FooterView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	private void initFooterView(Context context){  
        LinearLayout.LayoutParams lp = new LayoutParams(  
                LayoutParams.MATCH_PARENT, 0);  
        mFooter = LayoutInflater.from(context).inflate(R.layout.loader_footer, null);
        addView(mFooter, lp);  
          
        mArrow = (ImageView) mFooter.findViewById(R.id.ivLoaderArrow); 
        Globle.setHW(mArrow, 54, 80, context);
        mProgressBar = (ProgressBar) mFooter.findViewById(R.id.pbLoaderWaiting);  
        mLoaderTips = (TextView) mFooter.findViewById(R.id.loader_tips);  
          
        mRotateDown = new RotateAnimation(0.0f, 180.0f,    
                Animation.RELATIVE_TO_SELF, 0.5f,   
                Animation.RELATIVE_TO_SELF, 0.5f);  
        mRotateDown.setDuration(ROTATE_DURATION);  
        mRotateDown.setFillAfter(true);  
          
        mRotateUp = new RotateAnimation(180.0f, 0.0f,  
                Animation.RELATIVE_TO_SELF, 0.5f,   
                Animation.RELATIVE_TO_SELF, 0.5f);  
        mRotateUp.setDuration(ROTATE_DURATION);  
        mRotateUp.setFillAfter(true);  
          
        setFooterViewOptions(FOOTER_OPTIONS_PULL);  
    } 
	
	/**
	 * 设置更新效果方案：1、拖动；2、点击
	 * 
	 * @param options
	 */
	public void setFooterViewOptions(int options){  
        sFooterOps = options;  
          
        switch(sFooterOps){  
        case FOOTER_OPTIONS_PULL:  
            hide();  
            break;  
              
        case FOOTER_OPTIONS_CLICK:  
            show();  
            break;  
              
        default:  
            break;  
        }  
    }  
    
	/**
	 * 获取更新方式
	 * @return	sFooterOps
	 */
    public int getFooterViewOptions(){  
        return sFooterOps;  
    }  
    
    /**
     * 设置更新状态：1、未拖动；2、拖动未松手；3、松手后
     * @param state
     */
    public void setFooterState(int state){  
        if(mState == state){  
            return;  
        }  
          
        mArrow.clearAnimation();  
        if(state == STATE_LOADING){  
            mProgressBar.setVisibility(View.VISIBLE);  
            mArrow.setVisibility(View.GONE);  
        }else{  
            mProgressBar.setVisibility(View.GONE);  
            mArrow.setVisibility(View.VISIBLE);  
        }  
          
        switch(state){  
        case STATE_NORMAL:  
            mArrow.startAnimation(mRotateUp);  
            mLoaderTips.setText(R.string.pull_up_for_more);  
            break;  
              
        case STATE_WILL_RELEASE:  
            mArrow.startAnimation(mRotateDown);  
            mLoaderTips.setText(R.string.release_for_more);  
            break;  
              
        case STATE_LOADING:  
            mLoaderTips.setText(R.string.loading);  
            break;  
          
        default:  
            break;  
        }  
        mState = state;  
    }  
    
    /**
     * 获取更新状态
     * @return
     */
    public int getCurrentState(){  
        return mState;  
    }  
    
    /**
     * 设置 高度
     * @param height
     */
    public void setFooterHeight(int height){  
        if(height <= 0){  
            height = 0;  
        }  
          
        LayoutParams lp = (LayoutParams) mFooter.getLayoutParams();  
        lp.height = height;  
        mFooter.setLayoutParams(lp);  
    }  
      
    public int getFooterHeight(){  
        return mFooter.getHeight();  
    }  
    /**
     * 拖动更新效果 —— 显示箭头图标  
     */
    public void hide(){  
        mArrow.clearAnimation();  
        mArrow.setVisibility(View.VISIBLE);  
        mLoaderTips.setText(R.string.pull_up_for_more);  
        setFooterHeight(0);  
    }  
    /**
     * 点击更新效果 —— 隐藏箭头图标  
     */
    public void show(){  
        mArrow.clearAnimation();  
        mArrow.setVisibility(View.GONE);  
        mLoaderTips.setText(R.string.click_for_more);  
          
        LayoutParams lp = (LayoutParams) mFooter.getLayoutParams();  
        lp.height = LayoutParams.WRAP_CONTENT;  
        mFooter.setLayoutParams(lp);  
    }
	
}
